<script setup>
import { useSlots, ref } from 'vue';

defineProps({
    header: String,
    modelValue: Boolean,
})

const emit = defineEmits(["update:modelValue","confirm"])
const wrapper = ref(null);
const slots = useSlots()

function detectShowOrHide(e) {
    if (e.target == wrapper.value) {
        cancel();
    }
}

function cancel(){
    emit("update:modelValue", false)
}

function confirm(){
    emit("confirm");
    cancel();
}

</script>

<template>
    <div class="wrapper" v-if="modelValue" @click="detectShowOrHide" ref="wrapper">
        <div class="dialog">
            <div class="header">
                <slot name="header" v-if="slots['header']"></slot>
                <span>{{ header }}</span>
            </div>
            <div class="body">
                <slot></slot>
            </div>
            <div class="footer">
                <button variant="danger" @click="cancel">取消</button>
                <button variant="success" @click="confirm">确定</button>
            </div>
        </div>
    </div>
</template>

<style lang="scss" scoped>
.wrapper {
    background-color: #00000066;
    height: 100vh;
    width: 100vw;
    z-index: 1000;
    position: absolute;
    top: 0;
    left: 0;
    display: flex;
    justify-content: center;

    .dialog {
        background: white;
        align-self: center;
        width: 600px;
        border-radius: 6px;

        .header {
            height: 56px;
            border-bottom: 1px solid lightgray;
            padding: 13px 18px;
            font-size: 18px;
        }
        
        .footer {
            display: flex;
            border-top: 1px solid lightgray;
            padding: 13px 18px;
            justify-content: flex-end;
        }

        .body {
            padding: 20px;
        }
    }
}
</style>